package com.example.checkqufuface.Base;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;

import androidx.appcompat.app.AppCompatActivity;

import com.example.baselibrary.Base.MyApplication;
import com.example.baselibrary.Utils.Customer;
import com.example.baselibrary.Utils.LogUtil;
import com.example.baselibrary.Utils.SPUtils;
import com.example.baselibrary.Utils.StringUtil;
import com.example.checkqufuface.Customer.LoadingDialog;

import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


public abstract class BaseActivity extends AppCompatActivity {

    public String BASE_URL;
    public Map<String, String> headers;
    static ArrayList<Activity> activities;

    public  LoadingDialog.Builder loadBuilder;
    public  LoadingDialog dialog;

    public static LoadingDialog.Builder loadInBuilder;
    public static LoadingDialog dialogIn;
    public static Activity activityBase;
    Intent systemBarIntent = new Intent("com.tchip.changeBarHideStatus");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        //取消标题
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //取消状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(getView());
        activityBase = this;
        String url = (String) SPUtils.get(BaseActivity.this, Customer.QUFU_INPUT_URL, "");
        if (StringUtil.isNullOrEmpty(url)) {
            BASE_URL = Customer.QUFU_BASE_URL;
        } else {
            BASE_URL = url;
        }

        //获取headmap
        if (headers == null) {
            headers = new HashMap<>();
        }
        String token = (String) SPUtils.get(this, Customer.QUFU_ACCESSTOKEN, "");
        if (!StringUtil.isNullOrEmpty(token)) {
            headers.put("Token", token);
        }


        init();
        if (activities == null) {
            activities = new ArrayList<>();
        }
        addActivity();
    }

    protected abstract int getView();

    public abstract void init();

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        hideInput();
        return super.dispatchTouchEvent(ev);
    }


    /**
     * 隐藏键盘
     */
    protected void hideInput() {
        InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        View v = getWindow().peekDecorView();
        if (null != v) {
            imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
        }
    }

    public void addActivity() {
        activities.add(this);
        for (int i = 0; i < activities.size(); i++) {
            LogUtil.d("添加123：" + activities.get(i).getLocalClassName());
        }

    }


    public void removeActivity() {
        activities.remove(this);
    }

    /**
     * 结束所以活动
     */
    public void finishAllAcitivity() {
        for (Activity activity : activities) {
            if (!activity.isFinishing()) {
                activity.finish();
            }
        }
        execRootCmdSilent("settings put system systembar_hide 0");//1为隐藏导航栏，0显示导航栏
        this.sendBroadcast(systemBarIntent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        execRootCmdSilent("settings put system systembar_hide 0");//1为隐藏导航栏，0显示导航栏
        this.sendBroadcast(systemBarIntent);
    }

    @Override
    protected void onResume() {
        super.onResume();
        execRootCmdSilent("settings put system systembar_hide 1");//1为隐藏导航栏，0显示导航栏
        this.sendBroadcast(systemBarIntent);
    }

    private static int execRootCmdSilent(String cmd) {
        int result = -1;
        DataOutputStream dos = null;

        try {
            Process p = Runtime.getRuntime().exec("su");
            dos = new DataOutputStream(p.getOutputStream());
            dos.writeBytes(cmd + "\n");
            dos.flush();
            dos.writeBytes("exit\n");
            dos.flush();
            p.waitFor();
            result = p.exitValue();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (dos != null) {
                try {
                    dos.close();
                } catch (IOException e) {
//                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    public void DialogTipShow(String message) {
        if (loadBuilder == null) {
            loadBuilder = new LoadingDialog.Builder(this)
                    .setMessage(message)
                    .setCancelable(true)
                    .setCancelOutside(true);
        }
        if (dialog == null) {
            dialog = loadBuilder.create();
        }
        dialog.show();
    }

    public void DiglogTipDiss() {
        if (dialog != null) {
            dialog.dismiss();
        }
    }

    public static void DialogTipInShow(String message) {
        if (loadInBuilder == null) {
            loadInBuilder = new LoadingDialog.Builder(activityBase)
                    .setMessage(message)
                    .setCancelable(true)
                    .setCancelOutside(true);
        }
        if (dialogIn == null) {
            dialogIn = loadInBuilder.create();
        }
        dialogIn.show();
    }

    public static void DiglogTipInDiss() {
        if (dialogIn != null) {
            dialogIn.dismiss();
        }
    }
}
